<!--
 * @Description: 活动 - 疯狂星期五活动设置
 * @Author: liyujie
 * @Date: 2021/05/28
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/05/28
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    疯狂星期五 - 活动设置
                </div>
            </div>
            <!-- /header -->
            <!--表单-->
            <div class="form-wrapper">
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    label-position="right"
                    ref="ruleForm"
                    label-width="186px"
                    class="form-wrap"
                    @submit.native.prevent
                >
                    <el-form-item label="活动招商规则">
                        <el-input
                            v-model="ruleForm.shop_rule"
                            type="textarea"
                            placeholder="请输入活动招商规则"
                            style="width: 940px;"
                            :autosize="{ minRows: 10}"
                        />
                    </el-form-item>
                    <el-form-item label="活动须知">
                        <el-input
                            v-model="ruleForm.shop_intro"
                            type="textarea"
                            placeholder="请输入活动须知"
                            style="width: 940px;"
                            :autosize="{ minRows: 10}"
                        />
                    </el-form-item>
                    <el-form-item label="活动状态">
                        <el-radio-group v-model="ruleForm.status">
                            <div class="status-wrapper">
                                <div class="status-item">
                                    <el-radio :label="1">开启</el-radio>
                                </div>
                                <div class="status-item">
                                    <el-radio :label="0">关闭</el-radio>
                                    <div
                                        class="date-wrapper"
                                        v-if="!ruleForm.status"
                                    >
                                        <div>选择活动关闭时间</div>
                                        <el-date-picker
                                            v-model="dateTimeRange"
                                            type="datetimerange"
                                            range-separator="-"
                                            start-placeholder="开始日期"
                                            end-placeholder="结束日期"
                                            :default-time="['00:00:00', '23:59:59']"
                                            size="medium"
                                            style="width: 360px;margin-left: 12px"
                                            value-format="timestamp"
                                        />
                                        <div class="tips">
                                            活动关闭期间，商家不能报名疯狂星期五活动，活动关闭起始时间建议为每周一0点，活动关闭结束时间为每周日23点59分59秒。
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="submit-view">
            <el-button
                size="medium"
                type="default"
                @click="handleRoute('crazyFridayGoodsVerify')"
            >
                返回
            </el-button>
            <el-button
                size="medium"
                type="primary"
                @click="handleSubmit()"
            >
                保存
            </el-button>
        </div>
    </div>
</template>

<script>
import {back} from '@/mixin/back';
import { deepClone } from "@/assets/js/utils";

export default {
    name: 'CrazyFridaySetting',
    data() {
        return {
            ruleForm: {
                // 活动招商规则
                shop_rule: '',
                // 活动须知
                shop_intro: '',
                status: 1,
                activity_close_start_time: '',
                activity_close_end_time: '',
            },
            rules: {
                title: [
                    {required: true, message: '请输入规则标题', trigger: 'change'}
                ],
            },
            dateTimeRange: [],
            authResult: {}
        };
    },
    mixins: [back],
    watch: {
        dateTimeRange(value) {
            this.ruleForm.activity_close_start_time = value ? value[0] / 1000 : '';
            this.ruleForm.activity_close_end_time = value ? value[1] / 1000 : '';
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        /**
         * @description 页面初始化
         */
        init() {
            this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '活动管理'},
                {title: '疯狂星期五', path: '/student/crazy-friday-goods-verify'},{title: '活动设置'}]);
            this.authResult = this.$route.meta.authResult;
            this.getDetail();
        },

        /**
         * 获取
         */
        getDetail() {
            this.$post('/student/shop_goods_activity_friday_sale%5Cget_config', {}, resp => {
                if (resp.code === 1) {
                    let data = resp.data;

                    if(data.activity_close_start_time && data.activity_close_end_time) {
                        data.status = 0;
                    } else {
                        data.status = 1;
                    }
                    if(data.activity_close_start_time && data.activity_close_end_time) {
                        this.dateTimeRange = [data.activity_close_start_time * 1000, data.activity_close_end_time * 1000];
                    }
                    this.ruleForm = data;
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 提交
         */
        handleSubmit() {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    let params = deepClone(this.ruleForm);

                    if(params.status) {
                        params.activity_close_start_time = params.activity_close_end_time = '';
                    }
                    if(params.status === 0 && params.activity_close_start_time === '') {
                        this.$notify({
                            title: '提示',
                            type: 'warning',
                            message: '请选择关闭时间'
                        });
                        return;
                    }

                    this.$post('/student/shop_goods_activity_friday_sale%5Cset_config', params, resp => {
                        if (resp.code === 1) {
                            this.$notify({
                                title: '提示',
                                message: '保存成功',
                                duration: 2000,
                                type: 'success'
                            });
                            this.getDetail();
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                duration: 2000,
                                type: 'warning'
                            });
                        }
                    });
                }
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;
    .submit-view {
        width: 100%;
        height: 68px;
        background: #FFFFFF;
        box-shadow: 0px -1px 4px 0px rgba(0, 0, 0, 0.1);
        position: absolute;
        bottom: 0;
        left: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 1;
        padding-left: 210px;
        .el-button {
            width: 120px;
            margin: 0 16px;
        }
    }

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        margin: 12px;
        box-sizing: border-box;
        background: #fff;
        overflow-y: auto;
        padding-bottom: 78px;
        &::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }

        .header {
            background: #FFFFFF;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            flex-direction: column;
            border-bottom: 1px solid #EBEEF5;

            .header-title {
                font-size: 16px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                height: 66px;
                position: relative;
                display: flex;
                align-items: center;
                padding: 0 32px;
            }
        }

        .form-wrapper {
            padding: 24px 32px;
            .status-wrapper {
                display: flex;
                flex-direction: column;
                margin-top: 12px;
                .status-item {
                    margin-bottom: 20px;
                    display: flex;
                    align-items: center;
                    font-size: 14px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #333333;
                    .date-wrapper {
                        display: flex;
                        align-items: center;
                        .tips {
                            font-size: 12px;
                            font-family: PingFangSC-Regular, PingFang SC;
                            font-weight: 400;
                            color: #999999;
                            margin-left: 12px;
                            width: 457px;
                            line-height: 18px;
                        }
                    }
                }
            }
        }
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0px 1px 0px 0px rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>

